package com.edu.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.edu.entiy.Book;
import com.edu.entiy.BookType;
import com.edu.entiy.History;
import com.edu.entiy.Page;
import com.edu.service.BookService;
import com.edu.service.BookTypeService;
import com.edu.service.HistoryService;
import com.edu.service.impl.BookServiceImpl;
import com.edu.service.impl.BookTypeServiceImpl;
import com.edu.service.impl.HistoryServiceImpl;
import com.edu.utils.PageTool;
import com.edu.utils.PaginationUtils;
import com.edu.utils.ResBean;
@WebServlet("/bookTypeServlet/*")
public class BookTypeServlet extends BaseServlet{
	private BookTypeService bookTypeService = new BookTypeServiceImpl();
	private BookService bookservice = new BookServiceImpl();
	private HistoryService historyService = new HistoryServiceImpl();
	//分页查询
	public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
		//获取传入的数据
       String _currentPage = request.getParameter("currentPage");
       String _pageSize = request.getParameter("pageSize");
       int currentPage = Integer.parseInt(_currentPage);
       int pageSize = Integer.parseInt(_pageSize);
       Page<BookType> page = bookTypeService.selectByPage(currentPage, pageSize);
       System.out.println(page);
	}
	//分页查询
	public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
		//获取传入的数据
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        PageTool<BookType> pageTool = bookTypeService.selectByPage(_currentPage, _pageSize);
        String pageResult =  PaginationUtils.getPagation(pageTool.getTotalCount(), pageTool.getCurrentPage(), pageTool.getPageSize(), "/bookTypeServlet/page");
        //存数据
        request.setAttribute("pageResult", pageResult);
        request.setAttribute("bookTypeList", pageTool.getRows());
        request.getRequestDispatcher("/admin/admin_booktype.jsp").forward(request, response);
	}
	//查询所有图书类型
	public void list(HttpServletRequest request, HttpServletResponse response) {
		List<BookType> typeList =  bookTypeService.selectBookTypes();
		request.setAttribute("typeList", typeList);
	}
	//查询所有类型
//	public void list(HttpServletRequest request, HttpServletResponse response) {
//		//获取传入的数据
//		String tid = request.getParameter("typeId");
//		String typeName = request.getParameter("typeName");
//		List<BookType> bookTypes =  bookTypeService.bookTypes(tid, typeName);
//	}
	//检查该分页类型存不存在
	public void checkTypeName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String typeName = request.getParameter("typeName");
		BookType bookType =  bookTypeService.selecBookTypeByTypeName(typeName);
		ResBean resBean = new ResBean();
		System.out.println(bookType);
		if(bookType != null || typeName.isEmpty()) {
			//存在该类型
			resBean.setCode(400);
			resBean.setMessage("图书类型不能使用");
		}else {
			//该类型不存在
			resBean.setCode(200);
			resBean.setMessage("图书类型可以使用");
		}
		String json = JSON.toJSONString(resBean);
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(json);
	}
	//增加一个图书类型
	public void addBookType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取参数
		String typeName = request.getParameter("typeName");
		//添加之前要先校验类型是否重复
		BookType bookType = bookTypeService.selecBookTypeByTypeName(typeName);
		//如果类型不存在,我们才添加,否则不添加
		if(bookType == null) {
			int count = bookTypeService.addBookType(typeName);
			System.out.println(count);
			//这里是对添加之后的操作进行判断处理
			if(count == 1) {
				System.out.println("添加成功");
				request.getRequestDispatcher("/bookTypeServlet/page").forward(request, response);
			}else {
				System.out.println("添加失败");
			}
		}else {
			System.out.println("该类型已经存在!");
		}
	
	}
	//更改一个图书类型
	public void updateBookType(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException, ServletException, IOException {
		BookType bookType = new BookType();
		BeanUtils.populate(bookType, request.getParameterMap());
		
		//先检查修改之后的数据是否存在
		String newTypeName = bookType.getTypeName();
		BookType _bookType = bookTypeService.selecBookTypeByTypeName(newTypeName);
		if(_bookType == null) {
			System.out.println("该类型不存在");
			int count =  bookTypeService.updateBookType(bookType);
			request.getRequestDispatcher("/bookTypeServlet/page").forward(request, response);
		}else {
			System.out.println("该类型已经存在");
		}
	}
	//删除一个图书类型
	public void deleteBookType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取数据
		String _typeId = request.getParameter("typeId");
		int typeId = Integer.parseInt(_typeId);
		int result = bookTypeService.deleteBookType(typeId);
		//删除之前先查询该书籍类型是否有书
		Book book = bookservice.selectByCard(typeId);
		if(book == null) {
			if(result == 1) {
				System.out.println("删除成功");
			bookTypeService.deleteBookType(typeId);
			request.getRequestDispatcher("/bookTypeServlet/page").forward(request, response);
			}
			}else {
			System.out.println("删除失败");
			request.setAttribute("TypeError",1);
			request.getRequestDispatcher("/bookTypeServlet/page").forward(request, response);
			return;
		}
//		if(result == 1) {
//			System.out.println("删除成功");
//			request.getRequestDispatcher("/bookTypeServlet/page").forward(request, response);
//		}else {
//			System.out.println("删除失败");
//		}
	}
}
